package edu.cmu.cs.cs214.hw9.client.gui;

import java.awt.*;
import java.text.DateFormat;
import javax.swing.*;

import edu.cmu.cs.cs214.hw9.datatypes.Status;

/**
 * Implements a JList cell renderer that can be used to render user statuses. 
 */
public class StatusRenderer implements ListCellRenderer {
	public Component getListCellRendererComponent(JList list,
			Object o, int index, boolean isSelected, boolean cellHasFocus) {
		// Swing component and layout init.
		Status status = (Status) o;
		JPanel result = new JPanel();
		result.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		result.setBackground(new Color(59, 89, 152));
		
		// Labels for storing the status, username and date.
		JLabel nameLabel = new JLabel(status.getPoster().getFullname() + " posted: ");
		JLabel statusLabel = new JLabel(status.getMessage());
		JLabel dateLabel = new JLabel("Posted " + DateFormat.getDateInstance().format(status.getTimestamp()));

		// Make the font white.
		nameLabel.setForeground(Color.white);
		statusLabel.setForeground(Color.white);
		dateLabel.setForeground(Color.white);
		
		// Add our labels to the cell.
		c.gridx = 0;
		c.gridy = 0;
		result.add(nameLabel, c);
		
		c.gridx = 1;
		result.add(statusLabel, c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 2;
		result.add(dateLabel, c);
		
		return result;
	}
}
